// module_activity/pages/activity_list/detail/prize/prize.js
import Toast from "../../../../../miniprogram_npm/@vant/weapp/toast/toast"
import Dialog from "../../../../../miniprogram_npm/@vant/weapp/dialog/dialog"

const app = getApp()
const http = new app.appHttp()
const log = new app.appLogs()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: null,
    prize: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id
    })
    this.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 立即兑换
   * @param e
   */
  onExChange: function (e) {
    Dialog.alert({
      title: "奖品兑换",
      message: "兑换码：" + this.data.prize.cash_code
    }).then(() => {  })
  },

  /**
   * 获取数据
   */
  getData: function () {
    Toast.loading({
      duration: 0,
      message: "加载中..."
    })
    http.get("draw/record/detail",{
      record_id: this.data.id
    }).then(res => {
      if (res.data.code === http.REQUEST_OK) {
        res.data.data.cash_start_time = res.data.data.cash_start_time.substring(0,res.data.data.cash_start_time.length - 3)
        res.data.data.cash_end_time = res.data.data.cash_end_time.substring(0,res.data.data.cash_end_time.length - 3)
        this.setData({
          prize: res.data.data
        })
      }else {
        Dialog.alert({
          title: "获取数据失败",
          message: "网络异常，请稍后再试！"
        }).then(() => {
          wx.navigateBack()
        })
      }
      setTimeout(() => {
        Toast.clear()
      },500)
    }, err => {
      Toast.clear()
      Dialog.alert({
        title: "获取数据失败",
        message: "网络异常，请稍后再试！"
      }).then(() => {
        wx.navigateBack()
      })
    })
  },
})